<?php
  function announcements_form_validation($form) {
  
	//print_r($form); die;
    $stat = new set_static_variable();

	//check the corresponding concerned dept name
	//chk_concerned_dept($form['field_anndept']);
    //Duplication in title

    //check_title_duplication($form['title'], $form['type']['#value'], $form['field_anndept'], 'field_anndept', $stat->get_nid());
	
	/*District Validation*/
   	distvalidation($form['field_anndist']['und']['#value']);
   
    $stat->set_nid(0);

    /*$dist_value = $form['field_anndist']['#value']['value'];
    if ($dist_value == '' || $dist_value == ' ') {
      form_set_error('field_anndist', t('%field_title field is required.', 
                                                array('%field_title' => $form['field_anndist']['#title'])));  
    }*/
	
	
	// maximum length validation for Title and Description Field
    maxlength_validation($form['title']['#value'], 255, $form['title']['#title'], 'title');
	
 	//echo"tt".$form['field_anndesc']['und'][0]['value']['#value'];
	
	// minimum length charcter   
  	minilength_validation($form['title']['#value'], 5, $form['title']['#title'], 'title');
  	minilength_validation($form['field_anndesc']['und'][0]['value']['#value'], 5, $form['field_anndesc']['und'][0]['value']['#title'],'field_anndesc');
	minilength_validation($form['field_annkword']['und'][0]['value']['#value'], 5, $form['field_annkword']['und'][0]['value']['#title'], 'field_annkword');
	
    // Check bad character validation for Title and Keywords field   
	
	
    chkbadchar($form['title']['#value'], $form['title']['#title'], 'title');
  	chkbadchar($form['field_anndesc']['und'][0]['value']['#value'], $form['field_anndesc']['und'][0]['value']['#title'], 'field_anndesc');
  	chkbadchar($form['field_annkword']['und'][0]['value']['#value'], $form['field_annkword']['und'][0]['value']['#title'], 'field_annkword');
  	chkbadchar($form['field_anntothers'][0]['value']['#value'], $form['field_annkword']['und'][0]['value']['#title'], 'field_anntothers');

    url_validation($form['field_annurl']['und'][0]['value']['#value']);
	
    $form_type = $form['field_annavail'];
	//echo"s". $form_type['value']['#value'];
	if ($form_type['value']['#value'] == 'OnlineAvailable') {
  	
    $reference_url = $form['field_annurl'][0]['value']['#value'];
	//$reference_url = url_validation($reference_url);
	
    if(isURL($reference_url) == 0 && $reference_url != '') {
      form_set_error('field_annurl', t('Not a valid URL given in %name field', array(
                                                   '%name' => $form['field_annurl']['und'][0]['value']['#value']['#title'])));    
    }
    elseif ($reference_url == '') {
      form_set_error('field_annurl', t(' %name field is required', array(
                                                   '%name' => $form['field_annurl']['und'][0]['value']['#value']['#title'])));
    }
  }
	elseif ($form_type['value']['#value'] == 'Download') {
    // Validating the file size   
    $field_obj = $form['field_act_file_upload']; 
	$file_allowed_obj = $form['field_form_allowedfiletype'];
    file_upload_validation($field_obj, $file_allowed_obj, 'field_act_file_upload');  
  }
  
//echo "ABC".$form['field_annstart']['und'][0]['value']['date']['#value'];
	
	// Date Validation
	//check_scheduler_date_expire($form['scheduler_settings']['unpublish_on']['date']['#value'],'Valid upto');
    Commencement_date($form['field_annstart']['und'][0]['value']['date']['#value'],$form['field_annstart']['und'][0]['#title'],'field_annstart');
	
    /* 
    ** Annauncement date validation 
    */
	
    $introduced_on = $form['field_annstart'][0]['value']['date']['#value'];
    $valid_upto = $form['scheduler_settings']['unpublish_on']['date']['#value'];

   /* $flag = 0;
    $flag += check_date_expire($form['field_annstart']);
    $flag += check_date_expire($form['field_annexpire']);
  */

    if ($introduced_on != "" && $valid_upto != "" && dateDiff("-", $valid_upto, $introduced_on) < 0 && $flag == 0) {
      form_set_error('field_annstart', t('%expire date is lesser than %start date', array('%expire' => 'Valid upto',
                                                                                    '%start' => $form['field_annstart']['#title'])));
      form_set_error('scheduler_settings',' ');    
    }

    
  }

  function announcements_presave_validation($node) {
    $reference_url = $node->field_annurl[0]['value'];
    $node->field_annurl[0]['value'] = url_validation($reference_url);
  } // function form_presave_validation()
     
